package cn.com.kingkit.mvc.action.business;

import java.io.Serializable;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.com.kingkit.mvc.controller.BaseAction;
import cn.com.kingkit.mvc.controller.IAction;
import cn.com.kingkit.mvc.exception.BusinessException;
import cn.com.kingkit.mvc.model.business.Bom;
import cn.com.kingkit.mvc.service.impl.business.BomService;
import cn.com.kingkit.mvc.util.PageController;

public class BomAction  extends BaseAction implements IAction,Serializable{
	private final static Log logger = LogFactory.getLog(BomAction.class);
	private PageController pager = null;
	private BomService service = null;
	private Bom bom = null;
	private List<Bom> bList = null; 
	
	public String execute(HttpServletRequest request,HttpServletResponse response, ServletConfig servletConfig){
		String act = request.getParameter("act");
		if(null != act){
			if("list".equals(act)){
				//取列表
				if("list".equals(act)){
					int totalRows = service.getRecordCount(new Bom());
					String URL = request.getRequestURI();
					this.pager.setURL(URL);
					this.pager.setTotalRowsAmount(totalRows);
					List<Bom> resourceList = service.list(new Bom(), this.pager.getPageStartRow(), pager.getPageSize(), null, true);
					pager.setData(resourceList);
					return getReturnURL(LIST);
				}
				//点了添加或者点了修改
				else if ("get".equals(act)) {	
					bom = service.get(bom);
					bList = service.listAll(new Bom());
					return getReturnURL(EDIT);
				}
				//保存表单
				else if ("save".equals(act)) {
					try {
						service.save(bom);
						return getReturnURL("getList");
					} catch (BusinessException e) {
						//保存原来表单已输入的内容
						request.setAttribute("bom", bom);
						addMessage(e.getMessage());
						return getReturnURL(EDIT);
					}
				}
			}
		}
		//删除
		else if ("del".equals(act)) {
			String[] bomIds = request.getParameterValues("bomId");
			
			for (String Id : bomIds) {
				Bom b = new Bom();
				if(null != Id && !("".equals(Id))){
					//m.setMaterialId(materialId);if(null != materialId && !("".equals(materialId))){
					b.setId(Long.parseLong(Id));
					service.delete(b);
				}
			}
			return getReturnURL("getList");
		}	
		
		return null;
}
	
	public PageController getPager() {
		return pager;
	}

	public void setPager(PageController pager) {
		this.pager = pager;
	}

	public BomService getService() {
		return service;
	}

	public void setService(BomService service) {
		this.service = service;
	}

	public Bom getBom() {
		return bom;
	}

	public void setBom(Bom b) {
		this.bom = b;
	}

	public List<Bom> getbList() {
		return bList;
	}

	public void setBList(List<Bom> list) {
		bList = list;
	}
}
